package com.ruan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.entity.Category;
import com.ruan.entity.R;
import com.ruan.service.CategoryService;
import com.ruan.mapper.CategoryMapper;
import com.ruan.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 59499
* @description 针对表【category】的数据库操作Service实现
* @createDate 2023-09-08 14:38:30
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    @Transactional
    public R<Void> addCategory(Category category) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("name",category.getName());
        wrapper.eq("status",1);
        if (categoryMapper.selectOne(wrapper)!=null){
            return new R<>(100,"该商品类型已存在");
        }
        int row = categoryMapper.insert(category);
        if (row != 1){
            return new R<>(100,"商品类型添加失败");
        }
        return new R<>(200,"商品类型添加成功");
    }

    @Override
    public R<List<Category>> getCategoryList() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        List<Category> categoryList1 = categoryMapper.selectList(wrapper);
        return new R<>(200,"查询成功",categoryList1);
    }

    @Override
    public R<Void> editCategory(String id, String name) {
        Category category = categoryMapper.selectById(id);
        if (category.getName().equals(name)){
            return new R<>(100,"修改的商品类型名不能一样");
        }
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        List<Category> categoryList = categoryMapper.selectList(wrapper);
        if (!categoryList.isEmpty()){
            return new R<>(100,"该商品名已存在");
        }
        category.setName(name);
        int row = categoryMapper.updateById(category);
        if (row != 1){
            return new R<>(100,"修改失败");
        }
        return new R<>(200,"修改成功");
    }

    @Override
    public R<Void> disableCategoryById(Integer id) {
        Category category = categoryMapper.selectById(id);
        category.setStatus(0);
        int row = categoryMapper.updateById(category);
        if (row != 1){
            return new R<>(100,"禁用失败");
        }
        return new R<>(200,"禁用成功");
    }

    @Override
    public R<Void> enableCategoryById(Integer id) {
        Category category = categoryMapper.selectById(id);
        category.setStatus(1);
        int row = categoryMapper.updateById(category);
        if (row != 1){
            return new R<>(100,"启用失败");
        }
        return new R<>(200,"启用成功");
    }

    @Override
    public R<List<Category>> getAllCategoryList() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        List<Category> categoryList1 = categoryMapper.selectList(wrapper);
        return new R<>(200,"查询成功",categoryList1);
    }
}




